#ifndef _RAND_GENERATOR_H_
#define _RAND_GENERATOR_H_
#include <stdarg.h>

class RandGenerator
{
    unsigned int _seed;

    static RandGenerator *_instance;

public:
    RandGenerator(void)
    {
        _seed = time(NULL);
    }

    unsigned int next_int(void)
    {
    	_seed++;
        return rand_r(&_seed);
    }

    double next_double(void)
    {
    	_seed++;
        return (double)rand_r(&_seed) / RAND_MAX;
    }

    static RandGenerator *get_instance(void)
    {
        return _instance;
    }

    unsigned int get_number_from_array_by_random(int count, ...)
    {
        va_list ap; // 初始化指向可变参数列表的指针
        unsigned int num = 0;
        int index = next_int() % count;
        va_start(ap, count); // 将第一个可变参数的地址付给ap，即ap指向可变参数列表的开始
        for (int i = 0; i < (index + 1); ++i)
        {
        	num = va_arg(ap, int);
        }
        va_end(ap); // ap付值为0，没什么实际用处，主要是为程序健壮性
        return num;
    }

    int get_work_rate_value(int x)
    {
    	return (1. + (double)(x - 50) * 0.005) * 1000.;
    }
};



#define RAND_DBL RandGenerator::get_instance()->next_double()
#define RAND_INT RandGenerator::get_instance()->next_int()
#define RAND_ARRAY(x, args...) RandGenerator::get_instance()->get_number_from_array_by_random(x, ##args)
#define NEW_WORK_RATE_VALUE(x) RandGenerator::get_instance()->get_work_rate_value(x)

#define MY_MAX(x,y)	x>y?x:y

#endif /* _RAND_GENERATOR_H_ */
